import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { NzNotificationService } from 'ng-zorro-antd';
import { Settings } from 'http2';

/**
 * 用户令牌
 */
@Component({
  selector: 'app-system-token',
  templateUrl: './token.component.html',
})
export class SystemTokenComponent implements OnInit {
  auth_btn_del = false;
  url = `/cf/admin/token/page`;
  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[] = [
    { title: '序号', type: 'no' },
    { title: '用户ID', index: 'user_id' },
    { title: '用户名', index: 'username' },
    { title: '令牌', index: 'access_token' },
    { title: '类型', index: 'token_type' },
    { title: '过期时间(s)', index: 'expires_in' },
    {
      title: '操作',
      buttons: [
        {
          text: '删除',
          type: 'del',
          pop: {
            title: '确定删除用户token？该用户将被登出！',
            okType: 'danger',
          },
          click: (record, _modal, comp) => {
            this.http.delete(`/cf/admin/token/${record.access_token}`).subscribe(res => {
              if (res.status === 200) {
                this.notice.success('成功', `成功删除令牌`);
                comp!.load();
              }
            });
          },
          iif: (item: STColumn) => this.auth_btn_del,
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private setting: SettingsService,
  ) {}

  ngOnInit() {
    this.auth_btn_del = this.setting.user.permissions.sys_token_del;
  }
}
